package com.shop.loan.proxy;

import cn.hutool.core.date.DateUtil;
import com.shop.loan.mapper.LoanOrderBillByMybatisMapper;
import com.shop.loan.model.StatisticsByDay;
import com.shop.loan.model.entity.LoanOrderBillByMybatis;
import com.shop.loan.model.entity.LoanOrderByMybatis;
import org.springframework.stereotype.Component;
import xyz.erupt.annotation.fun.DataProxy;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.Map;

@Component
public class StatisticsByDayProxy implements DataProxy<StatisticsByDay> {

    @Resource
    LoanOrderBillByMybatisMapper loanOrderBillByMybatisMapper;

    @Override
    public void afterFetch(Collection<Map<String, Object>> list) {
        LoanOrderByMybatis loanOrderByMybatis;
        for (Map<String, Object> stringObjectMap : list) {
            String dayStr = stringObjectMap.get("day").toString();
            Integer type = (Integer) stringObjectMap.get("type");
            loanOrderByMybatis = new LoanOrderByMybatis();
            loanOrderByMybatis.setStartTime(DateUtil.beginOfDay(DateUtil.parse(dayStr)).toString());
            loanOrderByMybatis.setEndTime(DateUtil.endOfDay(DateUtil.parse(dayStr)).toString());
            loanOrderByMybatis.setUserType(type);
            LoanOrderBillByMybatis moneySum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
            BigDecimal money = BigDecimal.ZERO, payMoney = BigDecimal.ZERO, payMoneyByAll = BigDecimal.ZERO, payMoneyByPart = BigDecimal.ZERO, discountMoney = BigDecimal.ZERO;
            if (moneySum != null) {
                money = moneySum.getMoney().add(moneySum.getRateMoney()).add(moneySum.getShopMoney());
                payMoney = moneySum.getPayMoney();
                discountMoney = moneySum.getDiscountsMoney();
            }
            Integer orderNum = loanOrderBillByMybatisMapper.selectCount(loanOrderByMybatis);
            //还款订单数
            loanOrderByMybatis.setMoney(BigDecimal.ONE);
            Integer orderNumByPay = loanOrderBillByMybatisMapper.selectCount(loanOrderByMybatis);

            //结清订单数
            loanOrderByMybatis.setPayStatus(1);
            Integer orderNumByEnd = loanOrderBillByMybatisMapper.selectCount(loanOrderByMybatis);
            moneySum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
            if (moneySum != null) {
                //结清
                payMoneyByAll = moneySum.getMoney().add(moneySum.getRateMoney());
            }

            loanOrderByMybatis.setPayStatus(0);
            moneySum = loanOrderBillByMybatisMapper.selectSum(loanOrderByMybatis);
            if (moneySum != null) {
                //部分还款
                payMoneyByPart = moneySum.getMoney().add(moneySum.getRateMoney());
            }

            orderNumByPay = orderNumByPay == null ? 0 : orderNumByPay;
            orderNum = orderNum == null ? 0 : orderNum;
            orderNumByEnd = orderNumByEnd == null ? 0 : orderNumByEnd;
            //订单回款率
            BigDecimal orderRateByPay = BigDecimal.valueOf(orderNumByPay).divide(BigDecimal.valueOf(orderNum == 0 ? 1 : orderNum), 2, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));
            //金额回款率
            BigDecimal payRate = payMoney.divide(money.doubleValue() == 0 ? BigDecimal.ONE : money, 2, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100));

            stringObjectMap.put("orderNum", orderNum);
            stringObjectMap.put("orderNumByPay", orderNumByPay);
            stringObjectMap.put("orderNumByEnd", orderNumByEnd);
            stringObjectMap.put("orderRateByPay", orderRateByPay);

            stringObjectMap.put("money", money);
            stringObjectMap.put("payMoney", payMoney);
            stringObjectMap.put("payMoneyByAll", payMoneyByAll);
            stringObjectMap.put("payMoneyByPart", payMoneyByPart);
            stringObjectMap.put("discountMoney", discountMoney);
            stringObjectMap.put("payRate", payRate);

        }
    }
}
